import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class Applet extends JApplet implements ActionListener{
	JLabel nameLabel = new JLabel("Name");
	JLabel timeLabel = new JLabel("Time");
	JLabel toLabel = new JLabel(" to ");
	JLabel colonLabel1 = new JLabel(":");
	JLabel colonLabel2 = new JLabel(":");
	JLabel dateLabel = new JLabel("Date");
	JLabel slashLabel1 = new JLabel("/");
	JLabel slashLabel2 = new JLabel("/");
	JLabel descriptionLabel = new JLabel("Description");
	
	JTextField nameField = new JTextField("", 20);
	JTextField startHourTimeField = new JTextField("HH", 2);
	JTextField startMinuteTimeField = new JTextField("MM", 2);
	JTextField endHourTimeField = new JTextField("HH", 2);
	JTextField endMinuteTimeField = new JTextField("MM", 2);
	JTextField dateMonthField = new JTextField("MM", 2);
	JTextField dateDayField = new JTextField("DD", 2);
	JTextField dateYearField = new JTextField("YYYY", 4);
	JTextArea descriptionArea = new JTextArea("", 15, 15);
	JScrollPane descriptionPane = new JScrollPane(descriptionArea);
	
	JButton createButton = new JButton("Create Event");
	
	Container contentPane = this.getContentPane();
   SpringLayout layout = new SpringLayout();
	
	String name;
	int startHourTime;
	int startMinuteTime;
	int endHourTime;
	int endMinuteTime;
	int month;
	int day;
	int year;
	String description;
	
	boolean validEvent = true;
	boolean validStartHourTime = true;
	boolean validStartMinuteTime = true;
	boolean validEndHourTime = true;
	boolean validEndMinuteTime = true;
	boolean validDay = true;
	boolean validMonth = true;
	boolean validYear = true;
	boolean validName = true;
	boolean allValid = true;


   public void init() {
   	contentPane.setLayout(layout);
		contentPane.setSize(70, 70);
      contentPane.add(nameLabel);
      contentPane.add(nameField);
		contentPane.add(timeLabel);
		contentPane.add(startHourTimeField);
		contentPane.add(colonLabel1);
		contentPane.add(startMinuteTimeField);
		contentPane.add(toLabel);
		contentPane.add(endHourTimeField);
		contentPane.add(colonLabel2);
		contentPane.add(endMinuteTimeField);
		contentPane.add(dateLabel);
		contentPane.add(dateMonthField);
		contentPane.add(slashLabel1);
		contentPane.add(dateDayField);
		contentPane.add(slashLabel2);
		contentPane.add(dateYearField);
		contentPane.add(descriptionLabel);
		contentPane.add(descriptionPane);
		contentPane.add(createButton);
		createButton.addActionListener(this);
		createButton.setActionCommand("create");
		
		
		layout.putConstraint(SpringLayout.WEST, nameLabel, 20, SpringLayout.WEST, contentPane);
		layout.putConstraint(SpringLayout.NORTH, nameLabel, 20, SpringLayout.NORTH, contentPane);
		layout.putConstraint(SpringLayout.WEST, nameField, 5, SpringLayout.EAST, nameLabel);
		layout.putConstraint(SpringLayout.NORTH, nameField, 20, SpringLayout.NORTH, contentPane);

		layout.putConstraint(SpringLayout.WEST, timeLabel, 20, SpringLayout.WEST, contentPane);
		layout.putConstraint(SpringLayout.NORTH, timeLabel, 20, SpringLayout.SOUTH, nameLabel);
		layout.putConstraint(SpringLayout.WEST, startHourTimeField, 5, SpringLayout.EAST, timeLabel);
		layout.putConstraint(SpringLayout.NORTH, startHourTimeField, 20, SpringLayout.SOUTH, nameLabel);
		layout.putConstraint(SpringLayout.WEST, colonLabel1, 2, SpringLayout.EAST, startHourTimeField);
		layout.putConstraint(SpringLayout.NORTH, colonLabel1, 20, SpringLayout.SOUTH, nameLabel);
		layout.putConstraint(SpringLayout.WEST, startMinuteTimeField, 2, SpringLayout.EAST, colonLabel1);
		layout.putConstraint(SpringLayout.NORTH, startMinuteTimeField, 20, SpringLayout.SOUTH, nameLabel);
		layout.putConstraint(SpringLayout.WEST, toLabel, 5, SpringLayout.EAST, startMinuteTimeField);
		layout.putConstraint(SpringLayout.NORTH, toLabel, 20, SpringLayout.SOUTH, nameLabel);
		layout.putConstraint(SpringLayout.WEST, endHourTimeField, 5, SpringLayout.EAST, toLabel);
		layout.putConstraint(SpringLayout.NORTH, endHourTimeField, 20, SpringLayout.SOUTH, nameLabel);
		layout.putConstraint(SpringLayout.WEST, colonLabel2, 2, SpringLayout.EAST, endHourTimeField);
		layout.putConstraint(SpringLayout.NORTH, colonLabel2, 20, SpringLayout.SOUTH, nameLabel);
		layout.putConstraint(SpringLayout.WEST, endMinuteTimeField, 2, SpringLayout.EAST, colonLabel2);
		layout.putConstraint(SpringLayout.NORTH, endMinuteTimeField, 20, SpringLayout.SOUTH, nameLabel);
		
		layout.putConstraint(SpringLayout.WEST, dateLabel, 20, SpringLayout.WEST, contentPane);
		layout.putConstraint(SpringLayout.NORTH, dateLabel, 20, SpringLayout.SOUTH, timeLabel);
		layout.putConstraint(SpringLayout.WEST, dateMonthField, 5, SpringLayout.EAST, dateLabel);
		layout.putConstraint(SpringLayout.NORTH, dateMonthField, 20, SpringLayout.SOUTH, timeLabel);
		layout.putConstraint(SpringLayout.WEST, slashLabel1, 2, SpringLayout.EAST, dateMonthField);
		layout.putConstraint(SpringLayout.NORTH, slashLabel1, 20, SpringLayout.SOUTH, timeLabel);
		layout.putConstraint(SpringLayout.WEST, dateDayField, 2, SpringLayout.EAST, slashLabel1);
		layout.putConstraint(SpringLayout.NORTH, dateDayField, 20, SpringLayout.SOUTH, timeLabel);
		layout.putConstraint(SpringLayout.WEST, slashLabel2, 2, SpringLayout.EAST, dateDayField);
		layout.putConstraint(SpringLayout.NORTH, slashLabel2, 20, SpringLayout.SOUTH, timeLabel);
		layout.putConstraint(SpringLayout.WEST, dateYearField, 2, SpringLayout.EAST, slashLabel2);
		layout.putConstraint(SpringLayout.NORTH, dateYearField, 20, SpringLayout.SOUTH, timeLabel); 

		layout.putConstraint(SpringLayout.WEST, descriptionLabel, 20, SpringLayout.WEST, contentPane);
		layout.putConstraint(SpringLayout.NORTH, descriptionLabel, 20, SpringLayout.SOUTH, dateLabel);
		layout.putConstraint(SpringLayout.WEST, descriptionPane, 5, SpringLayout.EAST, descriptionLabel);
		layout.putConstraint(SpringLayout.NORTH, descriptionPane, 20, SpringLayout.SOUTH, dateLabel);

		layout.putConstraint(SpringLayout.WEST, createButton, 40, SpringLayout.WEST, contentPane);
		layout.putConstraint(SpringLayout.EAST, createButton, 40, SpringLayout.EAST, contentPane);
		layout.putConstraint(SpringLayout.SOUTH, createButton, 30, SpringLayout.SOUTH, contentPane);
		layout.putConstraint(SpringLayout.NORTH, createButton, 20, SpringLayout.SOUTH, descriptionPane);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if (e.getActionCommand().equals("create"))
		{
			this.name = nameField.getText();
			this.startHourTime = Integer.parseInt(startHourTimeField.getText());
			this.startMinuteTime = Integer.parseInt(startMinuteTimeField.getText());
			this.endHourTime = Integer.parseInt(endHourTimeField.getText());
			this.endMinuteTime = Integer.parseInt(endMinuteTimeField.getText());
			this.month = Integer.parseInt(dateMonthField.getText());
			this.day = Integer.parseInt(dateDayField.getText());
			this.year = Integer.parseInt(dateYearField.getText());
			this.description = descriptionArea.getText();
		
			this.validateName();
			if (!this.validName)
			{
				descriptionArea.append("The name you entered was not valid.\n");
			}
		
			this.validateStartHour();
			this.validateStartMinute();
			this.validateEndHour();
			this.validateEndMinute();
			if (!this.validStartHourTime)
			{
				this.allValid = false;
				descriptionArea.append("The hour of the start time is not valid.\n");
			}
			if (!this.validStartMinuteTime)
			{
				this.allValid = false;
				descriptionArea.append("The minute of the start time is not valid.\n");
			}
			if (!this.validEndHourTime)
			{
				this.allValid = false;
				descriptionArea.append("The hour of the end time is not valid.\n");
			}
			if (!this.validEndMinuteTime)
			{
				this.allValid = false;
				descriptionArea.append("The minute of the end time is not valid.\n");
			}
		
			this.validateYear();
			this.validateMonth();
			
			if (this.validYear)
			{
				if (this.validMonth)
				{
					this.validateDay();
					
					if (!this.validDay)
					{
						this.allValid = false;
						descriptionArea.append("The day you entered was not valid.\n");
					}
				}
				
				else
				{
					this.allValid = false;
					descriptionArea.append("The month you entered was not valid.\n");
				}
			}
		
			else
			{
				this.allValid = false;
				descriptionArea.append("The year you entered was not valid.\n");
				
				if (!this.validDay)
				{
					descriptionArea.append("The month you entered was not valid.\n");
				}
			}
			

		}
			
	}
	
	public int getStartHourTime()
	{
	    return this.startHourTime;
	}
	
	public int getStartMinuteTime()
	{
		return this.startMinuteTime;
	}
	
	public int getEndHourTime()
	{
	    return this.endHourTime;
	}
	
	public int getEndMinuteTime()
	{
		return this.endMinuteTime;
	}
	
	public String getName()
	{
	    return this.name;
	}
	
	public int getMonth()
	{
		return this.month;
	}
	
	public int getDay()
	{
		return this.day;
	}
	
	public int getYear()
	{
		return this.year;
	}

	public String getDescription()
	{
		return this.description;
	}

	public void validateMonth()
	{
		if (this.month >= 1 && this.month <= 12)
		{
			this.validMonth = true;
		}
		
		else
		{
			this.validMonth = false;
		}
	}
	
	public void validateDay()
	{
		boolean isLeapYear = false;
	
		if (this.month == 1 || this.month == 3 || this.month == 5 || this.month == 7 || this.month == 8 || this.month == 10 || this.month == 12)
		{
			if (this.day >= 1 && this.day <= 31)
			{
				this.validDay = true;
			}
		}
		
		else if (this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11)
		{
			if (this.day >= 1 && this.day <= 30)
			{
				this.validDay = true;
			}
		}
		
		else if (this.month == 2)
		{
			if (this.year % 400 == 0)
			{
				isLeapYear = true;
			}
			
			else if (this.year % 100 == 0)
			{
				isLeapYear = false;
			}
			
			else if (this.year % 4 == 0)
			{
				isLeapYear = true;
			}
			
			if (isLeapYear)
			{
				if (this.day >= 1 && this.day <= 29)
				{
					this.validDay = true;
				}
			}
			
			else if (!isLeapYear)
			{
				if (this.day >= 1 && this.day <= 28)
				{
					this.validDay = true;
				}
			}
		}
			
		else
		{
			this.validDay = false;
		}
	}
	
	public void validateYear()
	{
		if (this.year > 0)
		{
			this.validYear = true;
		}
	
		else
		{
			this.validYear = false;
		}
	}
	
	public void validateStartHour()
	{
		if (this.startHourTime >= 1 && this.startHourTime <= 24)
		{
			this.validStartHourTime = true;
		}
		
		else
		{
			this.validStartHourTime = false;
		}
	}
	
	public void validateStartMinute()
	{
		if (this.startMinuteTime >= 0 && this.startMinuteTime <= 59)
		{
			this.validStartMinuteTime = true;
		}
		
		else
		{
			this.validStartMinuteTime = false;
		}
	}
	
	public void validateEndHour()
	{
		if (this.endHourTime >= 1 && this.endHourTime <= 24)
		{
			this.validEndHourTime = true;
		}
		
		else
		{
			this.validEndHourTime = false;
		}
	}
	
	public void validateEndMinute()
	{
		if (this.endMinuteTime >= 0 && this.endMinuteTime <= 59)
		{
			this.validEndMinuteTime = true;
		}
		
		else
		{
			this.validEndMinuteTime = false;
		}
	}
	
	public void validateName()
	{
		if (this.name.length() > 0)
		{
			validName = true;
		}
		
		else
		{
			validName = false;
		}
	}

}